package com.example.demo.bizhan.distributelock.service;

import com.example.demo.bizhan.distributelock.dao.ItemsDAO;
import com.example.demo.bizhan.distributelock.domain.Items;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.UUID;

/**
 * @description: xxx
 * @time: 2020/11/2 10:54
 * @author: baojinlong
 **/
@Service
public class ItemsService {
    @Autowired
    private ItemsDAO itemsDAO;


    public Items getItem(String itemId) {
        return itemsDAO.getOne(itemId);
    }

    public void save(Items items) {
        items.setId(UUID.randomUUID().toString());
        itemsDAO.save(items);
    }

    /**
     * 获取商品的库存量
     *
     * @param itemId
     * @return
     */
    public int getItemsCounts(String itemId) {
        return itemsDAO.getOne(itemId).getCounts();
    }

    public void reduceCount(String itemId, int count) {
        Items items = getItem(itemId);
        items.setCounts(items.getCounts() - count);
        itemsDAO.save(items);
    }
}
